package com.api.devices.service.impl;

import com.api.common.enums.DataSourceType;
import com.api.devices.domain.query.tracing.serialization.SerializationQuery;
import com.api.devices.domain.vo.tracing.serialization.SerializationVo;
import com.api.devices.domain.vo.tree.TreeVo;
import com.api.devices.mapper.SerializationMapper;
import com.api.devices.service.ISerializationService;
import com.api.framework.datasource.DynamicDataSourceContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SerializationServiceImpl implements ISerializationService {
    @Autowired
    SerializationMapper serializationMapper;
    @Override
    public List<Double> generates(TreeVo vo) {
        DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SLAVE.name());
        List<Double> dataList = new ArrayList<>();
        try {
            dataList = serializationMapper.generates(vo.getSerializationTabName(),vo);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            DynamicDataSourceContextHolder.clearDataSourceType();
        }
        return dataList;
    }

    @Override
    public List<SerializationQuery> scroe(SerializationVo vo) {
        DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SLAVE.name());
        vo.initTableName();
        List<SerializationQuery> dataList = new ArrayList<>();
        try {
            dataList = serializationMapper.scroe(vo);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            DynamicDataSourceContextHolder.clearDataSourceType();
        }
        return dataList;
    }
}
